package Algorithm.LinkedListProblem;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author cj
 * @Date 2021/11/24 19:47
 */
public class listPartition {

    static class Node {
        public int value;
        public Node next;

        public Node(int val) {
            this.value = val;
        }
    }

    // 3个列表，分别存储小于、等于、大于值，最后拼链表
    public static Node listPartition1(Node head, int pivot) {
        if (head == null) {
            return head;
        }
        List<Integer> smallList = new ArrayList<>();
        List<Integer> midList = new ArrayList<>();
        List<Integer> largeList = new ArrayList<>();
        Node cur = head;
        while (head != null) {
            if (head.value < pivot) {
                smallList.add(head.value);
            } else if (head.value == pivot) {
                midList.add(head.value);
            } else {
                largeList.add(head.value);
            }
            head = head.next;
        }
        head = cur; // head指回头节点
        for (int val : smallList) {
            cur.value = val;
            cur = cur.next;
        }
        for (int val : midList) {
            cur.value = val;
            cur = cur.next;
        }
        for (int val : largeList) {
            cur.value = val;
            cur = cur.next;
        }
        return head;
    }

    // 1个数组，利用快排中的partition，之后拼接
    public static Node listPartition2(Node head, int pivot) {
        if (head == null) {
            return null;
        }
        int n = 0;
        Node cur = head;
        while (cur != null) {
            n++;
            cur = cur.next;
        }
        Node[] nodeArr = new Node[n];
        for (int i = 0; i < n; i++) {
            nodeArr[i] = head;
            head = head.next;
        }
        arrPartition(nodeArr, pivot);
        // 拼接
        for (int i = 0; i < n - 1; i++) {
            nodeArr[i].next = nodeArr[i + 1];
        }
        nodeArr[n - 1].next = null;
        return nodeArr[0];
    }

    private static void arrPartition(Node[] nodeArr, int pivot) {
        int left = -1;
        int index = 0;
        int right = nodeArr.length;
        while (index != right) {
            if (nodeArr[index].value == pivot) {
                index++;
            } else if (nodeArr[index].value < pivot) {
                swap(nodeArr, ++left, index++);
            } else {
                swap(nodeArr, --right, index);
            }
        }
    }

    private static void swap(Node[] nodeArr, int a, int b) {
        Node tmpNode = nodeArr[a];
        nodeArr[a] = nodeArr[b];
        nodeArr[b] = tmpNode;
    }

    // 进阶（保证顺序，时间O(n)，空间O(1)）
    public static Node listPartition3(Node head, int pivot) {
        Node sH = null; Node sT = null; Node eH = null;
        Node eT = null; Node bH = null; Node bT = null;
        Node cur = head;
        while (cur != null) {
            if (cur.value < pivot) {
                if (sT == null) {
                    sH = cur;
                    sT = cur;
                } else {
                    sT.next = cur;
                    sT = cur;
                }
            }
            if (cur.value == pivot) {
                if (eH == null) {
                    eH = cur;
                    eT = cur;
                } else {
                    eT.next = cur;
                    eT = cur;
                }
            }
            if (cur.value > pivot) {
                if (bH == null) {
                    bH = cur;
                    bT = cur;
                } else {
                    bT.next = cur;
                    bT = cur;
                }
            }
            cur = cur.next;
        }
        if (sT != null) {
            sT.next = null;
        }
        if (eT != null) {
            eT.next = null;
        }
        if (bT != null) {
            bT.next = null;
        }
        // 拼接
        if (sT != null) { // 第一段有
            sT.next = eH;
            eT = eT == null ? sT : eT;
        }
        if (eT != null) {
            eT.next = bH;
        }
        return sH != null ? sH : (eH != null ? eH : bH);
    }

    public static void main(String[] args) {
        Node head = new Node(3);
        head.next = new Node(2);
        head.next.next = new Node(7);
        head.next.next.next = new Node(2);
        head.next.next.next.next = new Node(4);
        head = listPartition3(head, 3);
        while (head != null) {
            System.out.println(head.value);
            head = head.next;
        }
    }
}
